package cn.school.controller.sys;

import cn.school.common.annotation.SysLog;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.enums.BusinessType;
import cn.school.common.exception.ApiException;
import cn.school.common.utils.SecurityUtils;
import cn.school.common.utils.SysUserUtils;
import cn.school.sys.entity.SysTenantInfo;
import cn.school.sys.entity.SysUser;
import cn.school.sys.service.SysTenantInfoService;
import cn.school.sys.vo.SysTenantInfoVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author: lx
 * @date: 2023/4/20 16:27
 * @description:
 */
@RestController
@RequestMapping("/sys/tenant")
@Api(tags = "Sys-tenant => 租户管理")
public class SysTenantInfoController {

    @Autowired
    SysTenantInfoService sysTenantInfoService;

    @ApiOperation("新增租户")
    @PreAuthorize("@ss.hasPermi('system:tenant:add')")
    @SysLog(title = "租户管理", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public CommonResult<Integer> create(@RequestBody SysTenantInfo sysTenantInfo) {
        int count = sysTenantInfoService.create(sysTenantInfo);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed("租户编号重复");
    }

    @ApiOperation("分页查询租户列表")
//    @PreAuthorize("@ss.hasPermi('system:tenant:pageList')")
    @PostMapping("/findPageList")
    public CommonResult<CommonPage<SysTenantInfo>> findPageList(@RequestBody SysTenantInfoVo vo){
        IPage<SysTenantInfo> pageList = sysTenantInfoService.findPageList(vo);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("修改查询租户信息")
    @PreAuthorize("@ss.hasPermi('system:tenant:update')")
    @PutMapping("/update")
    public CommonResult<Integer> update(@RequestBody SysTenantInfo sysTenantInfo){
        int count = sysTenantInfoService.update(sysTenantInfo);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed("修改失败");
    }

    @ApiOperation("删除租户信息")
    @PreAuthorize("@ss.hasPermi('system:tenant:delete')")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Integer> delete(@PathVariable("id") Long id) throws Exception{
        SysUser user = SysUserUtils.getSysUser();
        boolean isAdmin = SecurityUtils.isAdmin(user.getId());
        if(isAdmin){
            throw new ApiException("无权删除!!!");
        }
        int count = sysTenantInfoService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed("修改失败");
    }

    // 根据租户id获取单个的租户的信息
    @ApiOperation("根据租户的id获取租户的信息")
//    @PreAuthorize("@ss.hasPermi('system:tenant:getById')")
    @GetMapping("/getTenantInfoById/{id}")
    public CommonResult<SysTenantInfo> getTenantInfoById(@PathVariable Long id) {
        SysTenantInfo sysTenantInfo = sysTenantInfoService.getTenantInfoById(id);
        return CommonResult.success(sysTenantInfo);
    }

    // admin切换租户更改用户表租户id
    @ApiOperation("admin切换租户更改用户表租户id")
    @PreAuthorize("@ss.hasPermi('system:tenant:switch')")
    @PostMapping("/switchTenant")
    public CommonResult<Integer> switchTenant(@RequestBody SysTenantInfo sysTenantInfo) {
        int num = sysTenantInfoService.updateSysTenantId(sysTenantInfo);
        if (num == -1) {
            return CommonResult.failed();
        }
        return CommonResult.success();
    }
}
